<template>
	<div id="elementNew" class="elementNew">
		<group>
			<x-input title="类型名" v-model="value"></x-input>
			<x-textarea ref="textArea" title="类型说明" v-model="value_description" placeholder="在这里写点什么" :show-counter="false" :height="250"></x-textarea>
		</group>

		<div class="error" v-show="error">{{error}}</div>

		<flexbox>
			<flexbox-item>
				<x-button class="flexBtn_confirm" @click.native="confirm">确定</x-button>
			</flexbox-item>
			<flexbox-item>
				<x-button class="flexBtn_cancel" @click.native="cancel">取消</x-button>
			</flexbox-item>
		</flexbox>
	</div>
</template>

<script>
	import bus from '@/event/eventBus'
	import NetWorking from '../../../utils/networking'
	import {Group, XInput, XTextarea, XButton, Flexbox, FlexboxItem} from 'vux'
	export default {
		name: "new-list-of-value",
		components: {
			Group,
			XInput,
			XTextarea,
			XButton,
			Flexbox,
			FlexboxItem
		},
		data() {
			return {
				id: this.$route.params.id,
				value: '',
				sequence: '',
				value_description: '',
				title: this.$route.params.title,
				error: ''
			}
		},
		created: function () {
			bus.$emit("barController", 1)
			bus.$emit("setTopSimpleTitle", this.title)

			let _this = this
			NetWorking.doGet(
				'list_of_value/' + this.id,
				null,
				null
			).then(
				response => {
					let rs = response.data
					if(rs.error_code == 0){
						_this.value = rs.list_of_value.value
						_this.value_description = rs.list_of_value.value_description
					}else{
						alert(rs.error_message)
					}
				}
			)
		},
		methods: {
			cancel: function () {
				this.$router.go(-1)
			},
			confirm: function () {
				if(this.value == '' || this.value_description == ''){
					this.error = "请输入所有必填项"
					return
				}

				NetWorking.doPut(
					'list_of_value',
					null,
					{
						id: this.id,
						value: this.value,
						value_description: this.value_description,
					},
					null
				).then(
					response => {
						let rs = response.data
						if(rs.error_code == 0){
							this.$router.go(-1)
						}else{
							alert(rs.error_message)
						}
					}
				)
			}
		}
	}
</script>

<style>
	.elementNew {
		border: solid;
		border-color: #f7f7fa;
		margin-left: 5px;
		margin-right: 5px;
		margin-top: 56px;
		box-shadow: 5px 5px 5px #888888;
		border-radius: 25px;
		background-color: white;
	}

	.buttonRole {
		width: 100%;
		margin-top: 15px;
		border-radius: 25px;
	}
</style>